<?php
namespace Home\Controller;
use Common\Controller\CommonController;

class UserController extends CommonController {
	
	
	public function index(){
		$user = M('user');

		$keyword=I('key');
		$is_agent = I('is_agent', '');
		$type=I('type');
		if(!empty($keyword)){
			$where[$type] = array('like',"%".$keyword."%");
		}
		if($is_agent!==''){
			$where['is_agent'] = $is_agent;
		}
		$Count = $user->where($where)->count();
		$Page = new \Think\Pager($Count,10);
		$show=$Page->showhtml(); 

		$list=$user->alias('u')->join('__MEMBER_NO__ n ON u.id=n.user_id', 'LEFT')->where($where)->limit($Page->firstRow.",".$Page->listRows)->order('login_num desc,ctime desc')->field('u.*,n.member_no')->select();

		foreach ($list as $key => $value) {
			$uid=$value['id'];
			$where = array('uid' => $uid );
			$list[$key]['key']=$key+1;
			$shopInfo=M('shop')->where($where)->find();
			$list[$key]['shopname'] = $shopInfo['shopname'];
		}
		$this->assign('page',$show);
		$this->assign('list',$list);
		$this->display();
	}

    public function edit()
    {
        $id = I('id');
        if(IS_POST){
            $data = I('post.');
            $model = M('user');
            $data['agent_city'] = str_replace('/', ' ', $data['agent_city']);
            $res = $model->where(['id'=>$id])->save($data);
            if($res!==false){
                $this->ajaxReturn(array('s'=>true,'m'=>'修改成功', 'url'=>U('index')));
            }else{
                $this->ajaxReturn(array('s'=>false,'m'=>'修改失败!'));
            }
        }else{
            $info = M('user')->where(['id'=>$id])->find();
            // if($info['agent_city']){
            // 	$agent_city = explode(' ', $info['agent_city']);
            // }else{
            // }
            $this->assign('info', $info);
            $this->display();
        }
    }
}